package day4;

import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.CharsetUtil;

import java.net.InetSocketAddress;

public class EventLoopServer {
    public static void main(String[] args) throws InterruptedException {
        ChannelFuture channelFuture = new ServerBootstrap()
                .group(new NioEventLoopGroup())
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                super.channelRead(ctx, msg);
                                ByteBuf byteBuff = (ByteBuf) msg;
                                byteBuff.toString(CharsetUtil.UTF_8);
                            }
                        });
                    }
                })
//                .bind(new InetSocketAddress("localhost", 8080));
                .bind(new InetSocketAddress("localhost", 8080));
//        channelFuture.sync();
        Channel channel = channelFuture.channel();
        System.out.println(channel);
        System.out.println("");
        channel.writeAndFlush("hello world");
    }
}
